using System;
using System.Configuration;
using System.Web.Services.Protocols;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using CrmServiceWebReference;
using Resources;

public partial class Setup_WorkFlowSetup : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            workFlowList();
            queueList();
        }
    }

    private void workFlowList()
    {
        CrmService localService = Common.CrmAutorize();

        string xmlResults = "";
        try
        {
            xmlResults = localService.Fetch(Resource.XmlFetchWorkFlowProcessList);
        }
        catch (SoapException soapError)
        {
            Response.Write(soapError.Detail.InnerXml);
        }
        XmlDocument workFlowProcessList = new XmlDocument();
        workFlowProcessList.LoadXml(xmlResults);

        foreach (XmlNode node in workFlowProcessList.ChildNodes[0].ChildNodes)
        {
            DropDownList1.Items.Add(
                new ListItem(node.FirstChild.InnerText, node.SelectSingleNode("processid").InnerText));
            DropDownList2.Items.Add(
                new ListItem(node.FirstChild.InnerText, node.SelectSingleNode("processid").InnerText));
        }
    }

    private void queueList()
    {
        CrmService localService = new CrmService();
        localService.Credentials = Common.Credential();

        string xmlResults = localService.Fetch(Resource.XmlFetchQueueList);
        XmlDocument queueDoc = new XmlDocument();
        queueDoc.LoadXml(xmlResults);
        foreach (XmlNode node in queueDoc.ChildNodes[0].ChildNodes)
        {
            supportQueueSelection.Items.Add(
                new ListItem(node.SelectSingleNode("name").InnerText, node.SelectSingleNode("queueid").InnerText));
        }
    }

    private void loadCurrentWorkflows()
    {
        Guid workflow1 = new Guid(ConfigurationManager.AppSettings[Resource.Workflow1Id].ToString());
        Guid workflow2 = new Guid(ConfigurationManager.AppSettings[Resource.Workflow2Id].ToString());

        TargetRetrieveWFProcess target1 = new TargetRetrieveWFProcess();
        target1.EntityId = workflow1;

        TargetRetrieveWFProcess target2 = new TargetRetrieveWFProcess();
        target2.EntityId = workflow2;

        wfprocess workFlowProcess1 = (wfprocess) loadWorkFlow(target1).BusinessEntity;
        if (workFlowProcess1 != null)
        {
            CurrentWorkFlowLabel1.Text = workFlowProcess1.name;
            CurrentWorkFlowLabel1.ToolTip = workFlowProcess1.description;
        }

        wfprocess workFlowProcess2 = (wfprocess) loadWorkFlow(target2).BusinessEntity;
        if (workFlowProcess2 != null)
        {
            CurrentWorkFlowLabel2.Text = workFlowProcess2.name;
            CurrentWorkFlowLabel2.ToolTip = workFlowProcess2.description;
        }
    }

    private static RetrieveResponse loadWorkFlow(TargetRetrieveWFProcess workFlow)
    {
        CrmService localCrmService = new CrmService();
        localCrmService.Credentials = Common.Credential();

        RetrieveRequest retrieve = new RetrieveRequest();
        retrieve.Target = workFlow;
        retrieve.ColumnSet = new AllColumns();
        RetrieveResponse serverResponse = (RetrieveResponse) localCrmService.Execute(retrieve);
        return serverResponse;
    }

    protected void UpdateWorkFlow1Button_Click(object sender, EventArgs e)
    {
        Common.updateConfigFile(Resource.Workflow1Id, DropDownList1.SelectedValue);
        Response.Write("Set successfull");
    }

    protected void UpdateWorkFlow2Button_Click(object sender, EventArgs e)
    {
        Common.updateConfigFile(Resource.Workflow2Id, DropDownList2.SelectedValue);
        Response.Write("Set successfull");
    }

    protected void update(object sender, EventArgs e)
    {
    }

    protected void updateSupportQueueButton_Click(object sender, EventArgs e)
    {
        Common.updateConfigFile(Resource.AppSettingsSupportQueueId, supportQueueSelection.SelectedValue);
        Response.Write("Set successfull");
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    }
}